//
//  MMGTabBarController.swift
//  MyMicroblog
//
//  Created by Mac on 17/1/4.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class MMGTabBarController: UITabBarController {

    private var currentController: UIViewController?
   
    override func viewDidLoad() {
        super.viewDidLoad()

        // 为控制器的 tabBar 更换自定义视图
        let mmgTabBar = MMGTabBar(frame: tabBar.frame)
        
        mmgTabBar.composeClosure = { [weak self] in
        
            let composeCtr = MMGComposeViewController()
            
            self?.selectedViewController?.addChildViewController(composeCtr)
            self?.selectedViewController?.view.addSubview(composeCtr.view)
            composeCtr.view.frame = UIScreen.main.bounds
        }
        
        setValue(mmgTabBar, forKey: "tabBar")
        
        setupUI()
    }
    
    // 搭建 UI 界面
    private func setupUI() {
        // 生成系列子控制器
        let homeController = self.creatChildViewController(viewCtr: MMGHomeViewController(), title:"首页", imageName:"tabbar_home", imageNameSel:"tabbar_home_selected", index: 0)
        let messageController = self.creatChildViewController(viewCtr: MMGMessageViewController(), title:"消息", imageName:"tabbar_message_center", imageNameSel:"tabbar_message_center_selected", index: 1)
        let discoverController = self.creatChildViewController(viewCtr: MMGDiscoverViewController(),  title:"发现", imageName:"tabbar_discover", imageNameSel:"tabbar_discover_selected", index: 2)
        let profileController = self.creatChildViewController(viewCtr: MMGProfileViewController(), title:"我", imageName:"tabbar_profile", imageNameSel:"tabbar_profile_selected", index: 3)
        
        // 将生成的控制器组封装为数组
        let controllers = [homeController,messageController,discoverController,profileController]
        
        // 为 tabBarController 的子控制器赋值
        viewControllers = controllers
        selectedViewController = discoverController
    }
    
    
    private func creatChildViewController(viewCtr:MMGBaseViewController, title:String, imageName:String, imageNameSel:String, index:Int) -> MMGNavigationController {
        
        // 设置主题,根据需要是否直接设置
        viewCtr.title = title
        
        // 设置文字颜色
        viewCtr.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.lightGray], for: .normal)
        viewCtr.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        // 设置 tabBarItem 图片
        viewCtr.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        viewCtr.tabBarItem.selectedImage = UIImage(named: imageNameSel)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        viewCtr.tabBarItem.tag = index
        
        // 设置 badge 气泡
//        viewCtr.tabBarItem.badgeValue = "10";
        
        // 实例化导航控制器
        let navCtr: MMGNavigationController = MMGNavigationController(rootViewController: viewCtr)
        
        // 返回导航控制器
        return navCtr
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        // 获取 tabBar 的子视图
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    for imgView in subView.subviews {
                        if imgView.isKind(of: NSClassFromString("UIImageView")!) {
                            // 找到目标对象,实现动画效果
                            imgView.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.25, delay: 0.25, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { 
                                imgView.transform = .identity
                            }, completion: nil)
                        }
                    }
                }
                index += 1
            }
        }
    }
}
